package com.zcy.pmp.service.impl;

import cn.hutool.http.HttpResponse;
import com.zcy.auth.domain.EmployeeRole;
import com.zcy.auth.mapper.EmployeeRoleMapper;
import com.zcy.basic.data.ContextData;
import com.zcy.basic.service.impl.BaseServiceImpl;
import com.zcy.basic.util.EasyPoiUtil;
import com.zcy.pmp.domain.Employee;
import com.zcy.pmp.mapper.EmployeeMapper;
import com.zcy.pmp.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.swing.text.DateFormatter;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;

/**
 * <p>
 *
 * </p>
 * @author  lucker
 * @date  2023/11/28 14:52
 */

@Service
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private EmployeeRoleMapper employeeRoleMapper;

    @Transactional
    @Override
    public Long add(Employee employee) {
        Long add = employeeMapper.add(employee);
        EmployeeRole employeeRole = new EmployeeRole().setEmployeeId(employee.getId()).setRoleId(employee.getRole().getId());
        employeeRoleMapper.add(employeeRole);
        return add;
    }

    @Transactional
    @Override
    public int deleteById(Serializable id) {
        employeeRoleMapper.deleteByEmployeeId(id);
        return employeeMapper.deleteById(id);
    }

    @Transactional
    @Override
    public int deleteBatch(List<Long> tIdList) {
        employeeRoleMapper.deleteBatchByEmployeeIds(tIdList);
        return employeeMapper.deleteBatch(tIdList);
    }

    @Transactional
    @Override
    public void update(Employee employee) {
        employeeMapper.update(employee);
        if (employeeRoleMapper.updateByEmployee(employee) == 0) {
            EmployeeRole employeeRole = new EmployeeRole().setEmployeeId(employee.getId()).setRoleId(employee.getRole().getId());
            employeeRoleMapper.add(employeeRole);
        }
    }

    @Override
    public List<Employee> queryList(String keyword) {
        return employeeMapper.queryList(keyword);
    }

    @Transactional
    @Override
    public List<Employee> importEmployees(String token, MultipartFile file) {
        List<Employee> employees = EasyPoiUtil.importExcel(file, 1, 2, Employee.class);
//        System.out.println(employees);
        if (employees == null) return null;
        // 保存错误行
        List<Employee> errorEmployees = new ArrayList<>();
        List<Employee> trueEmployees = new ArrayList<>();
        for (Employee employee : employees) {
            if (!Pattern.matches("^(\\w+([-.][A-Za-z0-9]+)*){3,18}@\\w+([-.][A-Za-z0-9]+)*\\.\\w+([-.][A-Za-z0-9]+)*$", employee.getEmail())) {
                errorEmployees.add(employee);
            } else {
                trueEmployees.add(employee);
            }
        }
        employeeMapper.addBatch(trueEmployees);
        List<EmployeeRole> employeeRoles = new ArrayList<>();
        for (Employee trueEmployee : trueEmployees) {
            Long roleId = trueEmployee.getRole().getId();
            if (roleId != null) {
                employeeRoles.add(new EmployeeRole().setEmployeeId(trueEmployee.getId()).setRoleId(roleId));
            }
        }
        employeeRoleMapper.addBatch(employeeRoles);
        ContextData.ERROR_EXCEL = errorEmployees;
        return errorEmployees;
    }

    @Override
    public void export(List<Employee> employees, HttpServletResponse response, String filePrefix) {
        String dateStr = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd-HH-mm-ss"));
        EasyPoiUtil.exportExcel(employees,"员工","sheet1", Employee.class, filePrefix + dateStr + ".xlsx", response);
    }
}
